package com.braveman.im.ui.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.braveman.im.R;
import com.braveman.im.bean.DataListBean;
import com.braveman.im.bean.NoticeBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.NoticeAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class NoticeListFragment extends BaseFragment {
    private SwipeRefreshLayout mSwipeLayout;
    private LoadingLayout mLoadingLayout;
    private RecyclerView mLvActivityListMember;

    private List<DataListBean> dataList = new ArrayList<>();
    private NoticeAdapter noticeAdapter;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_notice_list, null);
    }

    @Override
    protected void initChildView() {
        mSwipeLayout = findViewById(R.id.swipe_layout);
        mLoadingLayout = findViewById(R.id.loading_layout);
        mLvActivityListMember = findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(mContext));
        noticeAdapter = new NoticeAdapter(R.layout.item_notice_details, dataList);
        mLvActivityListMember.setAdapter(noticeAdapter);
    }

    @Override
    protected void load() {
        doNetWorkGetGroupNotice();
    }

    private void doNetWorkGetGroupNotice() {
        ApiManager.getApi().getGroupNotice(1, 10).enqueue(new NetCallBack<NoticeBean>() {
            @Override
            protected void onSuccess(Call<NoticeBean> call, Response<NoticeBean> response) {
                if (response != null) {
                    NoticeBean noticeBean = response.body();

                    if (dataList != null) {
                        dataList.clear();

                    }
                    if (noticeBean != null) {
                        dataList = noticeBean.dataList;
                    }


                    if (dataList.size() == 0) {
                        mLoadingLayout.showEmpty();
                    } else {
                        mLoadingLayout.showContent();
                    }
                    mSwipeLayout.setRefreshing(false);
                } else {

                    mLoadingLayout.showError();
                    mSwipeLayout.setRefreshing(false);
                }

                if (noticeAdapter != null) {
                    noticeAdapter.notifyDataSetChanged();
                }
            }
        });
    }


    @Override
    protected void setChildViewListener() {
        noticeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });

        mLoadingLayout.setRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doNetWorkGetGroupNotice();
            }
        });
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doNetWorkGetGroupNotice();
            }
        });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }

}
